--[[ 编码: JX-40-12 名称: 取消作业 作者: HAN 日期:2025-1-29 级别:固定 (说明本段代码在项目中不太会变化) 函数: OperationCancel 功能: 把作业的状态设置为 取消 -- 需要判断 什么 作业的任务状态 变更记录: --]] jx_api = require ( "jx_external_api") wms_op = require ( "wms_operation") function OperationCancel( strLuaDEID ) local nRet, strRetInfo, operation_objs,n,m nRet, operation_objs = m3.GetSysDataJson( strLuaDEID ) if ( nRet ~=0 ) then lua.Error( strLuaDEID, debug.getinfo(1), objs ) end local nCount = #operation_objs if (nCount == 0) then return end local task_objs, wave_obj local task_code, cntr_code, task local operation = {} local strCondition local prompt_msg, msg, can_cancel local cancel_count = 0 -- 取消作业数量 prompt_msg = '' for n = 1, nCount do nRet, operation = m3.ObjAttrStrToLuaObj( "Operation", lua.table2str(operation_objs[n].attrs) ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "m3.ObjAttrStrToLuaObj 失败! "..operation ) end -- 只要是作业没完成的都可以取消 N_B_STATE = 2 (完成) 并且作业的任务状态(N_TASK_STATE) 不等于 2 -- 作业的任务状态=2 表示作业下面的任务已经设备开始执行了,这个时候必须和设备端进行一些交互后才能取消 if ( operation.bs_state ~= 2 and operation.task_state ~= 2 ) then can_cancel = true -- 查询作业下面的任务(状态 = 0等待 1已推送) strCondition = "S_OP_CODE = '"..operation.code.."' AND ( N_B_STATE = 0 OR N_B_STATE = 1 ) " nRet, task_objs = m3.QueryDataObject(strLuaDEID, "Task", strCondition) if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取任务信息失败!" .. operation.code) end if (task_objs ~= '') then for m = 1, #task_objs do task = m3.KeyValueAttrsToObjAttr( task_objs[m].attrs ) task_code = lua.Get_StrAttrValue( task.S_CODE ) cntr_code = lua.Get_StrAttrValue( task.S_CNTR_CODE ) if ( lua.Get_NumAttrValue( task.N_B_STATE ) == 1 ) then -- 1 任务已经已经推送给WCS,因此需要调用WCS接口进行取消 nRet, strRetInfo = jx_api.WCS_Cancel( strLuaDEID, cntr_code, task_code ) if ( nRet ~= 0 ) then msg = "编号 = '"..operation.code.."'的作业无法取消, 原因是WCS无法取消料箱号 = "..cntr_code.." 的任! 原因:"..strRetInfo prompt_msg = prompt_msg..msg.."\r\n" can_cancel = false end end end end if ( can_cancel ) then -- 把作业下面的‘待启动’,‘已推送’任务的状态设置为 5/取消 strCondition = "S_OP_CODE = '"..operation.code.."' AND ( N_B_STATE = 0 OR N_B_STATE = 1 ) " strUpdateSql = "N_B_STATE = 5" nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Task", strCondition, strUpdateSql ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "更新【Task】信息失败!"..strRetInfo ) end nRet, strRetInfo = wms_op.SetCancel( strLuaDEID, operation ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "更新【Operation】信息失败!"..strRetInfo ) end -- 检查作业来源,如果是入库波次,出库波次,他们的状态是关闭中 需要触发后台进行 强制完成操作 if ( operation.bs_type == "Inbound_Wave" ) then nRet, wave_obj = m3.GetDataObjectByKey(strLuaDEID, operation.bs_type, "S_WAVE_NO", operation.bs_no ) if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "获取'"..operation.bs_type.."'信息失败! " .. wave_obj ) end -- 如果入库波次状态=6/关闭中,需要后台触发强制完成事件 if ( wave_obj.b_state == 6 ) then local add_wfp = { wfp_type = 1, -- 触发数据对象事件(指定数据对象标识) cls = "Inbound_Wave", obj_id = wave_obj.id, obj_name = "入库波次号'"..operation.bs_no.."'-->强制完成", trigger_event = "强制完成" } nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "AddSysWFP失败!"..strRetInfo ) end end elseif ( operation.bs_type == "Outbound_Wave" ) then nRet, wave_obj = m3.GetDataObjectByKey(strLuaDEID, operation.bs_type, "S_WAVE_NO", operation.bs_no ) if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "获取'"..operation.bs_type.."'信息失败! " .. wave_obj ) end -- 如果出库波次状态 = 8/关闭中,需要后台触发强制完成事件 if ( wave_obj.b_state == 8 ) then local add_wfp = { wfp_type = 1, -- 触发数据对象事件(指定数据对象标识) cls = "Outbound_Wave", obj_id = wave_obj.id, obj_name = "出库波次号'"..operation.bs_no.."'-->强制完成", trigger_event = "强制完成" } nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "AddSysWFP失败!"..strRetInfo ) end end end cancel_count = cancel_count + 1 end end end -- 如果中间有作业无法取消的,前端显示原因 if ( prompt_msg ~= '' ) then mobox.setInfo( strLuaDEID, prompt_msg ) end local action if ( cancel_count == 1 ) then action = { { action_type = "refresh_cur_row", value = "" } } else action = { { action_type = "refresh", value = "" } } end nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action)) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "setAction错误: "..strRetInfo) end end